#define  _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
using namespace std;

namespace yjz
{
	template<class T, class Container = vector<T>, class Compare = less<T>>
	class priority_queue
	{
	public:
		void AdjustUp(size_t child)
		{
			Compare com;
			size_t parent = (child - 1) / 2;
			while (child > 0)
			{
				if (com(_con[parent], _con[child]))
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void AdjustDown(size_t parent)
		{
			Compare com;
			size_t child = 2 * parent + 1;
			while (child < size())
			{
				if ((child + 1) < size() && com(_con[child], _con[child + 1]))
				{
					++child;
				}
				if (com(_con[parent], _con[child]))
				{
					swap(_con[child], _con[parent]);
					parent = child;
					child = 2 * parent + 1;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}

		T& top()
		{
			return _con[0];
		}

		void pop()
		{
			swap(_con[size() - 1], _con[0]);
			_con.pop_back();
			AdjustDown(0);
		}

		size_t size() const
		{
			return _con.size();
		}

		bool empty() const
		{
			return _con.empty();
		}

	private:
		Container _con;
	};
}

int main()
{
	yjz::priority_queue<int, vector<int>, greater<int>> _pq;
	_pq.push(1);
	_pq.push(6);
	_pq.push(3);
	_pq.push(4);
	_pq.push(9);

	while (!_pq.empty())
	{
		cout << _pq.top() << " ";
		_pq.pop();
	}
	cout << endl;
	return 0;
}